package com.home.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.home.constants.Consts;
import com.home.jsonhelpers.JsonRequestObject;
import com.home.jsonhelpers.ResourceEntry;
import com.home.rhclient.MainController;
import com.home.rhtester.R;

public class ListResourcesActivity extends AdvanceActivity {

	private ListView resourceList;
	private long id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resourcelistlayout);
		resourceList = (ListView) findViewById(R.id.resourcelist);
		 id = savedInstanceState != null ? savedInstanceState.getLong("id")
				: -1;
		if (id == -1) {
			Bundle extras = getIntent().getExtras();
			id = extras != null ? extras.getLong("id") : -1L;
		}
		

		try {
			JsonRequestObject requestJsonObj = new JsonRequestObject(getApplicationContext());
			requestJsonObj.put(Consts.RCATEGORY_ID, id);
			MainController.GetInstance().restConnector.requestListofResoruces(requestJsonObj.getJsonObject(), resourceList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resourceList.setOnItemClickListener(new ResourceListener());
	}
	
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		
		super.onSaveInstanceState(outState);
		outState.putLong("id", id);
	}


	class ResourceListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			ResourceEntry entry=(ResourceEntry)parent.getAdapter().getItem(position);
			Intent intent = new Intent(ListResourcesActivity.this,
					ResourceActivity.class);
			
			
			intent.putExtra(Consts.RESOURCE_RAW_DATA,entry.rawData.toString());
			ListResourcesActivity.this.startActivityForResult(intent, 1);

		}

	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.v("onActivityResult", "result returned");
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode==Consts.RESULT_REFRESH_REQUESTED && requestCode==1)
		{
			try {
				JsonRequestObject requestJsonObj = new JsonRequestObject(getApplicationContext());
				requestJsonObj.put(Consts.RCATEGORY_ID, id);
				MainController.GetInstance().restConnector.requestListofResoruces(requestJsonObj.getJsonObject(), resourceList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listview_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.resfresh_list:
			try {
				JsonRequestObject requestJsonObj = new JsonRequestObject(getApplicationContext());
				requestJsonObj.put(Consts.RCATEGORY_ID, id);
				MainController.GetInstance().restConnector.requestListofResoruces(requestJsonObj.getJsonObject(), resourceList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		return true;
	}
	
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
